import {Injectable} from '@nestjs/common';
import {InjectRepository} from "@nestjs/typeorm";
import {Repository} from "typeorm";
import {AddressEntity} from "./address.entity";
import {ResultVo} from "../../vo/common/result.vo";

@Injectable()
export class AddressService {
    constructor(
        @InjectRepository(AddressEntity)
        private readonly addressRepository: Repository<AddressEntity>,
    ) {}

    async queryAll() {
        let data = await this.addressRepository.find()
        return ResultVo.successWithData(this.toTreeData(data,"0"))
    }

    toTreeData(data, pcode){
        function tree(code) {
            let arr = []
            data.filter(item => {
                return item.pcode === code;
            }).forEach(item => {
                let children = tree(item.code)
                arr.push({
                    text: item.name,
                    value: item.code,
                    children: children
                })
            })
            return arr
        }
        return tree(pcode)
    }

}
